package com.itheima.task;

/**
 * ThreadLocal : 线程安全,谁存的谁取!!!
 * set..存
 * get..取
 * remove..删
 *
 * Thread t = Thread.currentThread(); 获取当前线程对象
 * createMap(t, value);  把当前线程对象最为map的key,保证只能当前线程取
 *
 * @author zhangyan
 * @date 2021/4/1 21:01
 */
public class ThreadLocalDemo {

    private static ThreadLocal<String> local = new ThreadLocal<String>();

    public static void main(String[] args) {

        local.set("itcast"); //存储: 主线程

        //新建一个线程
        new Thread(new Runnable() {
            @Override
            public void run() {

                local.set("itheima");

                String s = local.get(); //获取: 子线程 --> itheima
                System.out.println("子线程:" + s);
            }
        }).start();

        String s = local.get(); //获取: 主线程 --> itcast
        System.out.println("主线程" + s);
    }
}
